package com.msg.xml;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * base class for manipulating the data in tables
 * different tables' manipulator will inherit it and implement their own behavior
 * @author jeson9603
 *
 */
public abstract class Table_Manipulator {
	protected String content;
	protected XMLHandler XMLhandler;
	protected DBHandler DBhandler;
	protected Property prop;
	
	
	protected Table_Manipulator(Property prop) {
		this.prop = prop;
		XMLhandler = XMLHandler.getInstace();
		DBhandler = DBHandler.getInstance();
	}
	
	/**
	 * read data from XML and store the data in the member variables
	 */
	public void readXML() {
		this.getSpecificData();
	}
	
	/**
	 * store the member variables into the database
	 */
	public void writeDB() {
		this.update();
	}
	
	/**
	 * get data from XML files, according to different implementation of manipulators, it will
	 * read different contents from XML file
	 */
	protected abstract String getSpecificData();
	
	/**
	 * save the data read from XML file into database
	 * @return : the 
	 */
	protected abstract String update();
	protected abstract void pushData(Object o);
	
	/**
	 * format the date string so that sql can accept it.
	 * @param str_Date : the date string need to be parsed and formatted
	 * @return : a formatted date string
	 */
	protected String formatDate(String str_Date) {
		String result = null;
		try {
			DateFormat formatter;
			formatter = new SimpleDateFormat("MMMM dd,yyyy  hh:ss a");
			Date date = (Date)formatter.parse(str_Date);
			SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			result = sdf.format(date);
		}
		catch(Exception ex) {
			ex.printStackTrace();
		}
		return result;
	}
	
}
